import Service from './service';

/**
 * 部件服务基类
 *
 * @export
 * @class ControlService
 * @extends {Service}
 */
export default class ControlService extends Service {

    /**
     * 部件模型
     *
     * @type {(any | null)}
     * @memberof ControlService
     */
    public mode: any | null = null;

    /**
     * Creates an instance of ControlService.
     * 
     * @param {*} [opts={}]
     * @memberof ControlService
     */
    constructor(opts: any = {}) {
        super(opts);
    }

    /**
     * 获取部件模型
     *
     * @returns {*}
     * @memberof ControlService
     */
    public getMode(): any {
        return this.mode;
    }

    /**
     * 处理返回数据
     *
     * @param {*} response
     * @returns
     * @memberof ControlService
     */
    public handleResponse(response: any) {
    }

    /**
     * 处理数据
     *
     * @memberof ControlService
     */
    public handleData(data: any = {}) {
        let mode: any = this.getMode();
        if (!mode && mode.getDataItems instanceof Function) {
            return data;
        }
        let item: any = {};
        let dataItems: any[] = mode.getDataItems();
        dataItems.forEach(dataitem => {
            let val = data.hasOwnProperty(dataitem.prop) ? data[dataitem.prop] : null;
            if (!val) {
                val = data.hasOwnProperty(dataitem.name) ? data[dataitem.name] : null;
            }
            item[dataitem.name] = val;
        });
        return item;
    }
}